package com.idp.web.shop.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.model.Response;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.shop.entity.Category;
import com.idp.web.shop.service.CategoryService;

/**
 * 商品分类controller
 * @time 2018年4月21日上午1:31:23
 * @author WuJiangWu
 */
@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController {

	private Logger logger = Logger.getLogger(CategoryController.class);

	@Resource
	private CategoryService categoryService;
	
	
	@RequestMapping("/init")
	public String init(){
		return "shop/category/categorySearch";
	}
	
	@RequestMapping("/list")
	public String list(Category category,Page<Category> page,HttpServletRequest request){
		try {
			List<Category> list = categoryService.findForTreeTable("");
			request.setAttribute("categoryList", list);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shop/category/categoryList";
	}

	/**
	 * 查询下级菜单
	 * @param parentId
	 * @return
	 */
	@RequestMapping("/treeTable")
	@ResponseBody
	public Response treeTable(String parentId){
		Response json = new Response();
		try {
			json.put("list",categoryService.findForTreeTable(parentId));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return json;
	}
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	新增修改页面初始化
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/category")
	public String category(String id,HttpServletRequest request){
		try {
			if(ValidateUtils.isNotEmpty(id)){
				
				Category category = categoryService.getById(id);
				request.setAttribute("category", category);
			}
		} catch (Exception e) {
			
			logger.error(e.getMessage(), e);
		}
		return "shop/category/category";
	}
	
	/**
	 * 获取类型树状结构
	 * @return
	 */
	@RequestMapping("/getCategoryForTree")
	@ResponseBody
	public Response getCategoryForTree(Long currentId){
		Response json = new Response();
		List<Category> categoryList = categoryService.find(new Category());
		
		if(categoryList != null && categoryList.size() > 0){
			List<Map<String, Object>> list = new ArrayList<>();
			for(Category category : categoryList){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", category.getId());
				map.put("name", category.getName());
				map.put("pId", category.getParentId());
				list.add(map);
			}
			json.put("list", list);
		}
		return json;
	}
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	保存
	 * @param category
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Response save(Category category){
		Response json = new Response();
		try {
			// 修改
			if(ValidateUtils.isNotEmpty(category.getId())){
				categoryService.update(category);
			}
			// 新增
			else{
				category.setCreateTime(new Date());
				categoryService.add(category);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setErrcode(-1);
			json.setMsg("保存失败");
		}
		return json;
	}
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Response delete(String id){
		Response json = new Response();
		try {
			categoryService.delete(id);
		} catch (Exception e) {
			
			logger.error(e.getMessage(), e);
			json.setErrcode(-1);
			json.setMsg("删除失败");
		}
		return json;
	}
}
